package appReate.tab;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import vuelta.general.R;
import vuelta.general.rss.ExpandableListAdapter;
import vuelta.general.rss.RssReader;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;

public final class TestFragment extends Fragment {
	// private static final String KEY_CONTENT = "TestFragment:Content";
	protected final Handler mHandler = new Handler();
	private JsonArrayArray[] mJobsArrayArray = new JsonArrayArray[7];

	class JsonArrayArray {
		private List<JSONObject> jobsArrayArray = new ArrayList<JSONObject>();

		public List<JSONObject> getmJobsArrayArray() {
			return jobsArrayArray;
		}

		public void setmJobsArrayArray(List<JSONObject> jobsArrayArray) {
			this.jobsArrayArray = jobsArrayArray;
		}

	}

	public static TestFragment newInstance(int position, Context context) {
		TestFragment fragment = new TestFragment();

		// StringBuilder builder = new StringBuilder();
		// for (int i = 0; i < 20; i++) {
		// builder.append(content).append(" ");
		// }
		// builder.deleteCharAt(builder.length() - 1);
		fragment.mPosition = position;
		fragment.mContext = context;

		return fragment;
	}

	private int mPosition = 0;
	private Context mContext;
	private ExpandableListAdapter adapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// if ((savedInstanceState != null) &&
		// savedInstanceState.containsKey(KEY_CONTENT)) {
		// mPosition = savedInstanceState.getString(KEY_CONTENT);
		// }

		View rowView = inflater.inflate(R.layout.rss, container, false);

		adapter = null;

		// Retrive the ExpandableListView from the layout
		ExpandableListView listView = (ExpandableListView) rowView.findViewById(R.id.listViewExp);
//		listView.setEmptyView(getActivity().findViewById(R.id.home_store_empty_list_progress));

		listView.setOnGroupClickListener(new OnGroupClickListener() {

			public boolean onGroupClick(ExpandableListView arg0, View arg1, int arg2, long arg3) {
				return false;
			}

		});

		adapter = new ExpandableListAdapter(mContext);
		listView.setAdapter(adapter);

		if (mJobsArrayArray.length != 0 && mJobsArrayArray[mPosition] != null
				&& mJobsArrayArray[mPosition].getmJobsArrayArray().size() != 0) {
			mHandler.post(new Runnable() {

				public void run() {
					adapter.setList(mJobsArrayArray[mPosition].getmJobsArrayArray());
				}
			});
		} else {
			getRssFeedArticle();
		}
		// mUpdateButton = (ImageButton) findViewById(R.id.update_button);
		// mUpdateButton.setOnClickListener(new OnClickListener() {
		//
		// public void onClick(View v) {
		// getRssFeedArticle();
		//
		// }
		// });

		return rowView;

	}

	private void getRssFeedArticle() {
		Thread t = new Thread(new Runnable() {

			private List<JSONObject> mJobsArray;

			public void run() {
				try {
					mJobsArray = RssReader.getLatestRssFeed(mPosition);
					if (mJobsArrayArray[mPosition] == null) {
						mJobsArrayArray[mPosition] = new JsonArrayArray();
					}
					mJobsArrayArray[mPosition].setmJobsArrayArray(mJobsArray);
				} catch (Exception e) {
//					Log.e("RSS ERROR", "Error loading RSS Feed Stream >> " + e.getMessage() + " //" + e.toString());
				}
				mHandler.post(new Runnable() {

					public void run() {
						adapter.setList(mJobsArray);
					}
				});
			}
		});
		t.start();

	}

	// @Override
	// public void onSaveInstanceState(Bundle outState) {
	// super.onSaveInstanceState(outState);
	// outState.putString(KEY_CONTENT, mPosition);
	// }
}
